
package com.tinkerlog {

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.events.Event;
import flash.geom.Matrix;
import flash.media.Camera;
import flash.media.Video;
import flash.system.Security;
import flash.system.SecurityPanel;
import flash.utils.setTimeout;

import mx.controls.Alert;

  public class WebCamJunk extends Sprite {
    public static const DEFAULT_CAMERA_WIDTH : Number = 160;
    public static const DEFAULT_CAMERA_HEIGHT : Number = 120;
    public static const DEFAULT_CAMERA_FPS : Number = 5;
    public var video:Video;
    public var camera:Camera;
    private var _cameraWidth : Number;
    private var _cameraHeight : Number;
    
    public function WebCamJunk(w:Number, h:Number) {
      //Alert.show(Camera.names.toString());
      camera = Camera.getCamera("0");
      
      camera.setQuality(0,100);
      
      ///////////shahid code ends
      _cameraWidth = w; // DEFAULT_CAMERA_WIDTH;
      _cameraHeight = h; // DEFAULT_CAMERA_HEIGHT;
      if (camera != null) {
      	        camera.setMode(_cameraWidth, _cameraHeight, DEFAULT_CAMERA_FPS,false);
        /*video = new Video(_cameraWidth, _cameraHeight);
        video.attachCamera(camera);
        addChild(video); 
      	Alert.show(camera.width+","+ camera.height,"Camera Hooooooooooooo");
      	Alert.show(video.width+", "+video.height,"Video info");*/
      	setTimeout(this.AttachedVideo,3000);
      }
      else {
        Security.showSettings(SecurityPanel.CAMERA)
      }
    }
    public function DetachedVideo():void{
    	//this.removeChild(this.video);
    	this.video.attachCamera(null);
    //	delete this.video;
    	//this.video = null;
    }
    public function AttachedVideo():void{
	 if(this.video ==null){
		video = new Video(_cameraWidth, _cameraHeight);
		this.addChild(this.video);
		}
		if ( this.camera!= null && this.video!=null){
        	video.attachCamera(camera);
        //addChild(video);  
        this.dispatchEvent(new Event("HeroZindabad"));
  		}   	
    }
    public function ResizeWebCam(pW:Number,pH:Number):void{
      _cameraWidth = pW; // DEFAULT_CAMERA_WIDTH;
      _cameraHeight = pH; // DEFAULT_CAMERA_HEIGHT;
        camera.setMode(_cameraWidth, _cameraHeight, DEFAULT_CAMERA_FPS);
        //video = new Video(camera.width, camera.height);
        //video.attachCamera(camera);
        //addChild(video);
        this.video.clear();
        this.removeChild(this.video);
        this.video = null
        this.video = new Video(pW,pH);
        
        this.video.attachCamera(this.camera);
        this.addChild(this.video);
    }
    public function ReleaseVideo():void{
    	this.video.attachCamera(null);
    	this.removeChild(this.video);
    	this.video = null;
    }
    public function getSnapshotBitmapData():BitmapData {
    	Alert.show(video.width+", "+video.height,"Video info");
    	Alert.show(_cameraWidth+", "+_cameraHeight,"Camera snap shot");
      var snapshot:BitmapData = new BitmapData(_cameraWidth, _cameraHeight);
      snapshot.draw(video,new Matrix());
      return snapshot;
    }
    public function getSnapshot():Bitmap {
      var bitmap : Bitmap = new Bitmap(getSnapshotBitmapData());
      return bitmap;
    }
  }
}
